﻿package com.saint.video.videoControls {
	
	import com.saint.utils.events.ButtonEvent;
	import com.saint.utils.Logger;
	import com.saint.video.data.VideoDataObject;
	import com.saint.video.events.VideoSliderEvent;
	import com.saint.video.SimpleVideo;
	import flash.display.Sprite;
	import com.saint.utils.events.UpdateEvent;
	import com.saint.utils.ui.GraphicBttn;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	public class VideoSlider extends Sprite {
		
		private var back:Sprite;
		private var streamed:Sprite;
		private var playHead:GraphicBttn;
		private var targetVideo:SimpleVideo;
		private var dragArea:Rectangle;
		
		private var trackHeight:uint;
		private var trackWidth:uint;
		private var onePercent:Number;
		private var _duration:Number = 0;
		private var _percentageLoaded:Number = 0;
		
		private var scrubberTimer:Timer;
		
		private var watchingPlayback:Boolean = true;
		
		public function VideoSlider( _width:Number):void {
			
			trackHeight = 6;
			trackWidth = _width;
			onePercent = trackWidth / 100;
			
			back = new Sprite();
			streamed = new Sprite();
			drawRect(back, trackWidth, trackHeight, 0xCCCCCC);
			drawRect(streamed, trackWidth, trackHeight, 0xC38787);
			
			streamed.scaleX = 0;
			
			playHead = new GraphicBttn(videoMark);
			playHead.addEventListener(ButtonEvent.BUTTON_PRESS, handleChange, false, 0, true);
			playHead.addEventListener(ButtonEvent.BUTTON_RELEASE, handleChange, false, 0, true);
			
			/* timer used when scrubbing to send position */
			scrubberTimer = new Timer(100);
			scrubberTimer.addEventListener(TimerEvent.TIMER, sendScrubPosition, false,0,true);
			
			back.y = streamed.y = (playHead.height - trackHeight) / 2
			
			addChild(back);
			addChild(streamed);
			addChild(playHead);
		}
		
		/**
		 * handles update events - Button actions
		 * @param	event
		 */
		private function handleChange(event:ButtonEvent) {
			switch(event.eventID) {
				case GraphicBttn.BUTTON_PRESS:
					updateDragArea();
					watchingPlayback = false;
					playHead.startDrag(false, dragArea);
					dispatchEvent(new VideoSliderEvent(VideoSliderEvent.SCRUBSTART));
					scrubberTimer.start();
					break;
				case GraphicBttn.BUTTON_RELEASE:
					scrubberTimer.stop();
					watchingPlayback = true;
					playHead.stopDrag();
					//targetVideo.seek((playHead.x * 100) / trackWidth);
					dispatchEvent(new VideoSliderEvent(VideoSliderEvent.SCRUBSTOP, (playHead.x * 100) / trackWidth ));
					break;
			}
		}
		
		/**
		 * sends scrubber position when scrubbing
		 *@param e:TimerEvent
		 */
		private function sendScrubPosition(e:TimerEvent):void
		{
			dispatchEvent(new VideoSliderEvent(VideoSliderEvent.SCRUBBING, (playHead.x * 100) / trackWidth ));
		}
		
		/**
		 * positions the playhead
		 * @param	time
		 */
		public function updatePlayHead(time:Number):void {
				playHead.x = ((time * 100) / _duration) * onePercent;
		}
		
		/**
		 * scales the streaming indicator
		 * @param	percent
		 */
		public function updateStreaming(percent:Number):void {
			_percentageLoaded = percent;
			streamed.scaleX = percentageLoaded;
		}
		
		/**
		 * sets the duration of the current video
		 * @param value
		 */
		public function set duration(value:Number):void {
			_duration = value;
		}
		
		/**
		 * scales the drag area used by the playhead, based on video currently streamed
		 */
		private function updateDragArea():void {
			dragArea = new Rectangle(0, 0, trackWidth * percentageLoaded, 0);
		}
		
		/**
		 * sets the percentage of the current netstream currently loaded
		 */
		private function get percentageLoaded():Number {
			return _percentageLoaded;
		}
		
		/**
		 * utility to draw a rectangle
		 *
		 * @TODO - move to graphic utils
		 *
		 * @param	target
		 * @param	_width
		 * @param	_height
		 * @param	colour
		 * @param	_alpha
		 */
		private function drawRect(target:Sprite, _width:uint, _height:uint, colour:uint=0x333333, _alpha:uint=100){
			target.graphics.beginFill(colour, _alpha);
			target.graphics.drawRect(0, 0, _width, _height);
		}
	}
	
}